package com.app.common.utils;

import java.io.IOException;
import java.io.InputStream;

import javax.xml.transform.TransformerException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.css.sword.kernel.utils.SwordLogUtils;
import com.sun.org.apache.xpath.internal.XPathAPI;

public class ParamConfig {
	private static SwordLogUtils log = SwordLogUtils.getLogger(ParamConfig.class);
	private static ParamConfig instance = null;
	private static Document PConfigDOM = null;
	
	private static Object obj = new Object();

	private ParamConfig() {
		try {
			InputStream iStream = this.getClass().getResourceAsStream("/ParamConfig.xml");
			PConfigDOM = XMLAdapter.load(iStream);
			iStream.close();
		} catch (IOException e) {
			log.error("不能读取相关参数的配置文件！" + e.getMessage());
		}
	}

	public static Document getPConfigDOM() {
		return PConfigDOM;
	}

	public static void setPConfigDOM(Document pConfigDOM) {
		PConfigDOM = pConfigDOM;
	}
	
	/**
	 * 实现单例模式
	 */
	public static ParamConfig getInstanse() {
		if (null == instance) {
			synchronized (obj) {
				if (null == instance) {
					instance = new ParamConfig();
					obj.notifyAll();
				}
			}
		}
		return instance;
	}
	
	public String getParam(String paramString) {
		String ParamVal = null;
		try {
			Element PCE = (Element) XPathAPI.selectSingleNode(PConfigDOM,
					"//ParamConfig/Param[@name='"+paramString+"']");
			ParamVal = PCE.getTextContent();
		} catch (TransformerException e) {
			e.printStackTrace();
		}
		return ParamVal==null?"":ParamVal.toString();
	}
	
}
